
#include "main.h"

bool        gb_IsSaveFlag;          // ��Ҫ�����־
/***************************************************************************************
* Function Name  : 
* Description    : ��EEPROM��ָ����ַ��Ԫд��һ���ֽ�ģ��
* Input          : addr = ��д���ֽڵ�ַ, byte= �����ֽڵ�����
* Return         : 
* Not            : 
***************************************************************************************/
void EEP_Byte_Write(unsigned int addr,unsigned char byte)
{  
    cli();                          // disable Interrupts during write
    FLASHCON = 1;
    XPAGE = (addr>>8)&0x07;         //ѡ����EEPROM����
    IB_OFFSET = addr&0xff;          //����̵Ĵ洢��Ԫ��8λ��ַ
    IB_DATA = byte;
    IB_CON1 = 0x6E;
    IB_CON2 = 0x05;
    IB_CON3 = 0x0A;
    IB_CON4 = 0x09;
    IB_CON5 = 0x06;
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    FLASHCON = 0;
    XPAGE = 0;
    sei();
}
/***************************************************************************************
* Function Name  : 
* Description    : ��EEPROM��д�����ɴ��������ӳ���
* Input          : writeAddr:д������ݵĴ�ŵ�ַ
                   pBuffer: ��д���ݵ���ʱ��ŵ�ַ����RAM�п��٣�
                   NumToWrite:��д�����ݵĸ�����
* Return         : 
* Not            : 
***************************************************************************************/
//void EEP_Write (unsigned int WriteAddr, unsigned char *pBuffer, unsigned char NumToWrite)
//{
//    unsigned char t;
//    for(t=0;t<NumToWrite;t++)
//    {
//        EEP_Byte_Write(WriteAddr+t,*pBuffer++);
//    }
//}
/***************************************************************************************
* Function Name  : 
* Description    : �߳�ָ����EEPROMҳ��256�ֽ�/ÿҳ���ӳ���
* Input          : addr = ������ַ
* Return         : 
* Not            : 
***************************************************************************************/
void EEP_Erase(unsigned int addr)
{
    cli();                          // disable Interrupts during write
    XPAGE = (addr>>8)&0x07;      
    FLASHCON = 1;
    IB_CON1 = 0xE6;
    IB_CON2 = 0x05;
    IB_CON3 = 0x0A;
    IB_CON4 = 0x09;
    IB_CON5 = 0x06;
    _nop_();
    _nop_();
    _nop_(); 
    FLASHCON = 0;
    XPAGE = 0;
    sei();
}
/***************************************************************************************
* Function Name  : 
* Description    : ��EEPROM��ָ����Ԫģ��
* Input          : addr:����ȡ���ݵĵ�ַ
* Return         : 
* Not            : 
***************************************************************************************/
unsigned char EEP_Byte_Read(unsigned int addr)
{
    unsigned char TEMPB;
   
    FLASHCON = 1;
    //XPAGE = 0;
    TEMPB = *(unsigned char code *)(addr);
    FLASHCON = 0;
    return(TEMPB);  // return data
}
/***************************************************************************************
* Function Name  : 
* Description    : ��EEPROM��ָ����Ԫģ��
* Input          : addr:����ȡ���ݵĵ�ַ
* Return         : 
* Not            : 
***************************************************************************************/
// unsigned short EEP_Word_Read(unsigned int addr)
// {
//     unsigned short TEMPB;
//    
//     FLASHCON = 1;
//     //XPAGE = 0;
//     TEMPB = *(unsigned short code *)(addr);
//     FLASHCON = 0;
//     return(TEMPB);  // return data
// }
/***************************************************************************************
* Function Name  : 
* Description    : ��EEPROM�ж������������ӳ���
* Input          : addr:����ȡ���ݵĵ�ַ
* Return         : ReadAddr:���������ݵ���ʱ��ŵ�ַ����RAM�п��٣�
                   pBuffer: ����ȡ���ݵ��׵�ַ
                   NumToRead:����ȡ���ݵĸ��� 
* Not            : 
***************************************************************************************/
//void EEP_Read(unsigned int ReadAddr,unsigned char *pBuffer,unsigned int NumToRead)
//{
//    while(NumToRead--)
//    {
//        *pBuffer++=EEP_Byte_Read(ReadAddr++); 
//    }
//}
/***************************************************************************************
* Function Name  : 
* Description    : 
* Input          : 
* Return         : 
* Not            : 
***************************************************************************************/
static u8 EepromCalcCRC(u8  *ptr, u8  len)
{
    u8  crc;
    u8  i;
    crc = 0;

    while(len--)
    {
        crc ^= *ptr++;

        for(i = 0; i < 8; i++)
        {
            if(crc & 0x01)
            {
                crc = (crc >> 1) ^ 0x8C;
            }
            else crc >>= 1;
        }
    }

    return crc;
}
/***************************************************************************************
* Function Name  : 
* Description    : 
* Input          : 
* Return         : 
* Not            : 
***************************************************************************************/
void EepromRead(void)
{
    u8 i;
    u8 crc;
    u8 buf[10];
    
    
    for(i=0; i<5; i++)
    {
        buf[i] = EEP_Byte_Read(EEPROM_DATA_ADDRESS+i);
    }
    //
    crc = EepromCalcCRC(&buf[0], 4);
    if( (buf[4] == crc)) 
    {	
        return;
    }
    

    
    gb_IsSaveFlag = 1;      // 
}
/***************************************************************************************
* Function Name  : 
* Description    : 
* Input          : 
* Return         : 
* Not            : 
***************************************************************************************/
void SaveDataProcess(void)
{
    if(gb_IsSaveFlag)       // 
    {
        u8 i;
        u8 buf[10];
        
        for(i=0; i<4; i++)
        {
            buf[i] = eeprom_rec.reg_char[i];
        }      
        buf[4] = EepromCalcCRC(&buf[0], 4);
        
        //
        EEP_Erase(EEPROM_DATA_ADDRESS);
        for(i=0; i<5; i++)
        {
            EEP_Byte_Write(EEPROM_DATA_ADDRESS+i, buf[i]);
        }          
        
        gb_IsSaveFlag = 0;
    }
}
/*********************************   END OF FILE   ************************************/